package com.v0710.docommon.aop;

import com.v0710.docommon.exception.SysException;
import com.v0710.docommon.tool.LoginTool;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @Author YIFan
 * @Date 2021-10-30 22:28
 * @Version 1.0
 */
@Slf4j
@Aspect
@Component
public class CheckLoginAop {

    public CheckLoginAop(){}

    /*切点路径*/
    public static final String AOP_PATH =
            "@within(com.v0710.docommon.annotation.CheckLogin) || " +
            "@annotation(com.v0710.docommon.annotation.CheckLogin)";

    /**
     * 声明AOP签名
     */
    @Pointcut(AOP_PATH)
    public void pointcut() {}

    /**
     * 环绕切入
     *
     * @param joinPoint 切面对象
     * @return 底层方法执行后的返回值
     * @throws Throwable 底层方法抛出的异常
     */
    @Around("pointcut()")
    public void around(ProceedingJoinPoint joinPoint){
        // 注解鉴权
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 从请求头得到token,获取token得key
        String tokenKey = LoginTool.setTokenKey(LoginTool.getHeaderToken());

        System.err.println("-----tokenKey "+ tokenKey);

        if (!LoginTool.getRedisTool().hasKey(tokenKey)) {
            throw new SysException("用户暂未登录,请登录");
        }
    }

}
